<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

/**
 * 网站文章栏目
 *
 * @icon fa fa-circle-o
 */
class Scolumn extends Backend
{
    
    /**
     * SiteColumn模型对象
     * @var \app\admin\model\SiteColumn
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Scolumn;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index(){
           // dump(input("get.fid"));
        if(input("get.flag")){
            $res=db("scolumn")->find(input("get.fid"));
            $fid=$res['fid'];
        }else{
            if(input("get.fid")){
                $fid=input("get.fid");
            }else{
                $fid=0;
            }
        }
        $columns=db("scolumn")->where("fid",$fid)->order("c_order desc")->select();
        $scolumn_ctype=getArr("scolumn-ctype");
        $this->assign("scolumn_ctype",$scolumn_ctype);
        $this->assign("columns",$columns);
        $this->assign("fid",$fid);

        if(request()->isPost()){
            $data=input("post.");

           // dump($data);die;
            foreach($data as $k=>$v){

                db("scolumn")->where("id",$k)->setField("c_order",$v);

            }

            $this->redirect("/admin/scolumn/index?fid={$fid}");



        }


        return $this->fetch();

    }

    public function index_add(){
        $fid=input("get.fid");
       // dump($fid);
        $scolumn_ctype=getArr("scolumn-ctype");
        $this->assign("scolumn_ctype",$scolumn_ctype);
        if(Request()->isPost()){

            $data=input("post.");
            $data['fid']=$fid;
            if($_FILES['c_pic']['tmp_name']){
                $img=$this->uploadImg();
                $data['c_pic']=$img['m_path'];
            }
            //dump($data);die;
            $add=db("scolumn")->insert($data);
            if($add){
                $this->success("数据添加成功","/admin/scolumn/index?fid={$fid}");
            }else{
                $this->error("数据添加失败");
            }
        }
        $this->assign("fid",$fid);
        return $this->fetch();
    }

    public function index_edit(){
        $id=input("get.id");
        $res=db("scolumn")->where("id",$id)->find();
        $scolumn_ctype=getArr("scolumn-ctype");
        $this->assign("scolumn_ctype",$scolumn_ctype);
        if(Request()->isPost()){
            $data=input("post.");
            $data['fid']=$res['fid'];

            if($_FILES['c_pic']['tmp_name']){
                $filePaths = ROOT_PATH . 'public' ;
                //dump($filePaths);
                if($res['c_pic']!=''){
                    $imgurl=$filePaths.$res['c_pic'];
                   // dump($imgurl);
                    @unlink($imgurl);
                }
                $str="c_pic";
                $img=$this->uploadImg($str);
                $data['c_pic']=$img['m_path'];

                 }else{
                    $data['c_pic']=$res['c_pic'];
                 }
               /// dump($data);die;
                $result=db("scolumn")->update($data);
                if($result){
                    $this->success("数据修改成功","/admin/scolumn/index?fid={$res['fid']}");
                }else{
                    $this->error("数据修改失败");
                }


        }
        $this->assign([
           'res'=>$res,
        ]);
        return $this->fetch();
    }


    public function uploadImg($str){
     ///  /./ dump($str);
       /// die;
        $file = request()->file($str);
        $filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS ;
        if(!file_exists($filePaths)){
            mkdir($filePaths,0777,true);
        }
        $info = $file->move($filePaths);
        // dump($info);
        if($info){
            $imgpath = $info->getPath()."/".$info->getBasename();//物理路径
            $image = \think\Image::open($imgpath);
            $date_path = $info->getPath()."/"."200".'_'."120";
            $imgpath=$info->getSaveName();//原图上传  路径： 日期/文件名
            if(!file_exists($date_path)){
                mkdir($date_path,0777,true);
            }
            $thumb_path = $date_path.'/'."1-".$info->getBasename();
            $image->thumb(200, 120)->save($thumb_path);
            $data['m_path'] = "/uploads/".$imgpath;
            return $data;
        }else{
            // 上传失败获取错误信息
            return $file->getError();
        }

    }


    public function index_del(){
        $id=input("get.id");
        $site_column=db("scolumn");
        $res=$site_column->where("id",$id)->find();
        $list=$site_column->select();
        $columns=$this->getAllSon($list,$res['id']);
        //dump($columns);die;
        $filePaths = ROOT_PATH . 'public' ;
        foreach($columns as $k=>$v){
            $list1=db("site_article")->where("column_id",$v['id'])->select();
            foreach($list1 as $k1=>$v1){
                $imgurl1=$filePaths.$v1['pic'];
                @unlink($imgurl1);
                db("site_article")->delete($v1['id']);
            }
            $res1=$site_column->where("id",$v['id'])->find();

            $imgurl=$filePaths.$res1['c_pic'];
            // dump($imgurl);
            @unlink($imgurl);
            $site_column->delete($v['id']);
        }
        $list2=db("site_article")->where("column_id",$id)->select();
        foreach($list2 as $k2=>$v2){
            $imgurl11=$filePaths.$v2['pic'];
            @unlink($imgurl11);
            db("site_article")->delete($v2['id']);
        }
        $result=$site_column->delete($id);
        if($result){
            $this->success("数据删除成功");
        }else{
            $this->error("数据删除失败");
        }

    }

    public function getAllSon($data,$fid){
        static $ids=[];
        foreach($data as $k=>$v){
            if($v['fid']==$fid){
                $ids[$k]['id']=$v['id'];
                //$ids[$k]['c_pic']=$v['c_pic'];
                $this->getAllSon($data,$v['id']);
            }
        }
        return $ids;


    }


    public function index_article(){
        $column_id=input("get.column_id");
        if(input("get.a_fid")){
            $a_fid=input("get.a_fid");
        }else{
            $a_fid=0;
        }

        $where['a_fid']=$a_fid;
        $where['column_id']=$column_id;
        $articles=db("site_article")->where($where)->order("a_order desc")->select();
        foreach($articles as $k=>$v){
            $res=db("scolumn")->field("c_title")->find($column_id);
            $articles[$k]['c_title']=$res['c_title'];
        }
        if(request()->isPost()){
            $data=input("post.");

            foreach($data as $k=>$v){

                db("site_article")->where("id",$k)->setField("a_order",$v);

            }

            $this->redirect("/admin/scolumn/index_article?column_id={$column_id}&a_fid={$a_fid}");

        }



        $this->assign([
            'a_fid'=>$a_fid,
             'column_id'=>$column_id,
            'articles'=>$articles,
        ]);
        return $this->fetch();
    }


    public function index_article_add(){
        $a_fid=input("get.a_fid");
        $column_id=input("get.column_id");
        $site_column=db("scolumn");
        $admin=$_SESSION['think']['admin'];
       // dump($admin);
        if(Request()->isPost()){

            $data=input("post.");
            $data['a_fid']=$a_fid;
            $data['fid']=0;
            $data['column_id']=$column_id;
            $data['userid']=$admin['id'];
            $data['username']=$admin['username'];
            $data['userpic']=$admin['avatar'];
            $data['ctime']=time();
            if($_FILES['pic']['tmp_name']){
                $str="pic";
                $img=$this->uploadImg($str);
                $data['pic']=$img['m_path'];
            }
            //dump($data);die;

            $result=db("site_article")->insert($data);

            if($result){
                $this->success("数据添加成功","/admin/scolumn/index_article?a_fid={$a_fid}&column_id={$column_id}");
            }else{
                $this->error("数据添加失败");
            }

        }
        $res=$site_column->field("c_title")->where("id",$column_id)->find();

        $this->assign([
            'res'=>$res,
            'a_fid'=>$a_fid,
            'column_id'=>$column_id,
        ]);
        return $this->fetch();
    }

    public function index_article_edit(){
        $id=input("get.id");
        $site_article=db("site_article");
        $site_column=db("scolumn");
        $resArticle=$site_article->field("*")->where("id",$id)->find();
        $res=$site_column->field("c_title")->where("id",$resArticle['column_id'])->find();

        if(Request()->isPost()){
            $data=input("post.");

            if($_FILES['pic']['tmp_name']){
                $filePaths = ROOT_PATH . 'public' ;
                //dump($filePaths);
                if($resArticle['pic']!=''){
                    $imgurl=$filePaths.$resArticle['pic'];
                    // dump($imgurl);
                    @unlink($imgurl);
                }
                $str="pic";
                $img=$this->uploadImg($str);
                $data['pic']=$img['m_path'];

            }else{
                $data['pic']=$resArticle['pic'];
            }
            $data['ctime']=time();
            /// dump($data);die;
            $result=db("site_article")->update($data);
            if($result){
                $this->success("数据修改成功","/admin/scolumn/index_article?column_id={$resArticle['column_id']}&a_fid={$resArticle['a_fid']}");
            }else{
                $this->error("数据修改失败");
            }


        }

        $this->assign([
            'resArticle'=>$resArticle,
            'res'=>$res
        ]);
        return $this->fetch();
    }


    public function index_article_del(){
        $id=input("get.id");
        $site_article=db("site_article");
        $res=$site_article->find($id);

        $filePaths = ROOT_PATH . 'public' ;
        $imgurl=$filePaths.$res['pic'];
        @unlink($imgurl);
        $result=$site_article->delete($id);
        if($result){
            $this->success("数据删除成功");
        }else{
            $this->error("数据删除失败");
        }

    }



    

}
